package controllers

import (
	"encoding/json"
	"log"
	"github.com/astaxie/beego"
	// "beego-restful-server/models"
	"io/ioutil"
)

type MainController struct {
	beego.Controller
}
// @Title Create
// @Description create object
// @Param	body		body 	models.Object	true		"The object content"
// @Success 200 {string} models.Object.Id
// @Failure 403 body is empty
// @router / [post]
func (o *MainController) Post() {
	log.Println("post:",o.GetString("objectId"),"-",o.Input().Get("objectId"),"-",o.Ctx.Input.Param(":objectId"))
	o.ServeJSON()
}

// @Title Get
// @Description find object by objectid
// @Param	objectId		path 	string	true		"the objectid you want to get"
// @Success 200 {object} models.Object
// @Failure 403 :objectId is empty
// @router /:objectId [get]
// localhost:8080/api/1
func (o *MainController) Get() {
	objectId := o.Ctx.Input.Param(":objectId")
	log.Println("ss:",objectId)	
	// if objectId != "" {
	// 	// ob, err := models.GetOne(objectId)
	// 	if err != nil {
	// 		o.Data["json"] = err.Error()
	// 	} else {
	// 		o.Data["json"] = ob
	// 	}
	// }
	o.ServeJSON()
}

// @Title Delete
// @Description delete the object
// @Param	objectId		path 	string	true		"The objectId you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 objectId is empty
// @router /:objectId [delete]
// localhost:8080/api/1
func (o *MainController) Delete() {
	objectId := o.Ctx.Input.Param(":objectId")
	// models.Delete(objectId)
	log.Println("delete",objectId,"ok")
	o.Data["json"] = "delete success!"
	o.ServeJSON()
}


type U struct{
	Id int64 `json:"id"`
	User string `json:"user"`
}
func (o *MainController) Put() {
	objectId := o.Ctx.Input.Param(":objectId")
	u:=U{}
	ss,_:=ioutil.ReadAll(o.Ctx.Request.Body)
	js:=json.Unmarshal(ss,&u)
	log.Println("Put",objectId,"ok",u,js,string(ss),"d")

	o.ServeJSON()
}
